package com.jkc.xh.fragments;


import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.View;
import android.widget.TextView;

import com.jkc.xh.R;
import com.jkc.xh.adapters.MyFactoryHoldAllAdapter;
import com.jkc.xh.adapters.core.BaseRecyclerAdapter;
import com.jkc.xh.constants.Constant;
import com.jkc.xh.fragments.core.BasePullRecyclerFragment;
import com.jkc.xh.manager.TraderManager;
import com.jkc.xh.manager.UserManager;
import com.jkc.xh.models.HoldAllModel;
import com.jkc.xh.network.RestCallBack;
import com.jkc.xh.network.models.HoldAllResponse;
import com.jkc.xh.network.models.RestError;
import com.jkc.xh.network.models.SystemHintResponse;
import com.jkc.xh.views.widget.ToastUtil;
import com.jkc.xh.views.widget.pullview.PullRecyclerView;

import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

/**
 * 我的持仓-分笔持仓
 */
@EFragment(R.layout.fragment_child_my_factory_all)
public class MyFactoryHoldAllFragment extends BasePullRecyclerFragment implements BaseRecyclerAdapter.OnViewClickListener {

    private MyFactoryHoldAllAdapter mAdapter;
    private int pageIndex = 0;
    private int pageTotal = 0;
    @ViewById
    TextView hintInfo;
    @Override
    protected void initView(PullRecyclerView recyclerView) {
        mAdapter = new MyFactoryHoldAllAdapter();
        mAdapter.setListener(this);
        mAdapter.setItemClickListener(mAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(mAdapter);
        requestData(true);

        addJob(UserManager.getCpmfHintConfig(new RestCallBack<SystemHintResponse>() {
            @Override
            public void success(SystemHintResponse obj) {
                hintInfo.setText(Html.fromHtml(obj.about_yqfy));
            }

            @Override
            public void failure(RestError error) {

            }
        }));
    }

    public void requestData(final boolean isRefresh) {
        if (isRefresh) {
            pageIndex = 0;
            if (getPullRecyclerView() != null) {
                getPullRecyclerView().autoRefresh();
            }
        } else {
            pageIndex++;
            if (pageIndex >= pageTotal) {
                return;
            }
        }
        addJob(TraderManager.queryHoldAllList(pageIndex,0, new RestCallBack<HoldAllResponse>() {
            @Override
            public void success(HoldAllResponse response) {
                pageTotal = response.pages;
                if (response.list == null || response.list.size() == 0) {
//                    ToastUtil.showToastShort("数据加载完毕");
                    finishLoad(false);
                    return;
                }
                if (mAdapter == null) {
                    return;
                }
                if (isRefresh) {
                    mAdapter.replaceWith(response.list);
                } else {
                    mAdapter.addAll(response.list);
                }
                if (response.list.size() < Constant.PAGE_SIZE) {
                    finishLoad(false);
                } else {
                    finishLoad(true);
                }

            }

            @Override
            public void failure(RestError error) {

            }
        }));

    }

    @Override
    protected void onRefresh(RecyclerView recyclerView) {
        requestData(true);
    }

    @Override
    protected void onLoadMore(RecyclerView recyclerView) {
        requestData(false);
    }

    @Override
    public void onItemClick(View view, Object object, int position) {
        HoldAllModel model = (HoldAllModel) object;
        switch (view.getId()) {
            case R.id.btnSold:
                if (model.isStockCanSell()) {
                    TradeSoldAllFragment_.builder().arg("modelExtra", model.stock_code).build().startFragment(getContext());
                } else {
                    ToastUtil.showToastShort("该持仓不可卖!");
                }
                break;
        }
    }
}
